using MediatR;
using Microsoft.AspNetCore.Mvc;
using Medical.ErrorCode;
using BaseData.ErrorCode;
using Medical.ReadApi.Appcontext.Command;
using Medical.ReadApi.Dto;

namespace Medical.ReadApi.Controllers
{
	/// <summary>
	/// 授权管理控制器（读操作）- 角色和权限查询
	/// </summary>
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class AuthorizationController : ControllerBase
	{
		private readonly IMediator _mediator;
		
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="mediator">MediatR中介者</param>
		public AuthorizationController(IMediator mediator)
		{
			_mediator = mediator;
		}

		/// <summary>
		/// 获取角色列表（支持分页和筛选）
		/// </summary>
		/// <param name="nameKeyword">角色名称关键词（模糊搜索）</param>
		/// <param name="isActive">是否激活状态筛选</param>
		/// <param name="page">页码（从1开始，默认值：1）</param>
		/// <param name="size">每页数量（默认值：10）</param>
		/// <returns>分页的角色列表</returns>
		/// <remarks>
		/// 示例请求:
		/// GET /api/Authorization/GetRoles?nameKeyword=管理员&amp;isActive=true&amp;page=1&amp;size=10
		/// </remarks>
		/// <response code="200">查询成功，返回分页的角色列表</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="500">服务器内部错误</response>
		[HttpGet]
		[ProducesResponseType(typeof(ApiResult<ApiPaging<RoleDto>>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<ApiPaging<RoleDto>>> GetRoles([FromQuery] string? nameKeyword = null, [FromQuery] bool? isActive = null, [FromQuery] int page = 1, [FromQuery] int size = 10)
		{
			try { var data = await _mediator.Send(new GetRolesListQuery { NameKeyword = nameKeyword, IsActive = isActive, Page = page, Size = size }); return new ApiResult<ApiPaging<RoleDto>> { Code = ApiEnum.查询成功, Message = "查询成功", data = data }; }
			catch (Exception ex) { return new ApiResult<ApiPaging<RoleDto>> { Code = ApiEnum.查询失败, Message = ex.Message, data = null }; }
		}

		/// <summary>
		/// 根据ID获取角色详情（包含权限列表）
		/// </summary>
		/// <param name="roleId">角色ID</param>
		/// <returns>角色详细信息，包含关联的权限ID列表</returns>
		/// <remarks>
		/// 示例请求:
		/// GET /api/Authorization/GetRoleDetail?roleId=1
		/// 
		/// 返回的角色信息包含：
		/// - 角色基本信息（名称、描述等）
		/// - 关联的权限ID列表
		/// </remarks>
		/// <response code="200">查询成功，返回角色详细信息</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">角色不存在</response>
		/// <response code="500">服务器内部错误</response>
		[HttpGet]
		[ProducesResponseType(typeof(ApiResult<RoleDto>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<RoleDto>> GetRoleDetail([FromQuery] long roleId)
		{
			try { var data = await _mediator.Send(new GetRoleDetailQuery { RoleId = roleId }); return new ApiResult<RoleDto> { Code = ApiEnum.查询成功, Message = "查询成功", data = data }; }
			catch (Exception ex) { return new ApiResult<RoleDto> { Code = ApiEnum.查询失败, Message = ex.Message, data = null }; }
		}

		/// <summary>
		/// 获取权限列表（支持分页和筛选）
		/// </summary>
		/// <param name="group">权限分组筛选</param>
		/// <param name="isActive">是否激活状态筛选</param>
		/// <param name="page">页码（从1开始，默认值：1）</param>
		/// <param name="size">每页数量（默认值：100）</param>
		/// <returns>分页的权限列表</returns>
		/// <remarks>
		/// 示例请求:
		/// GET /api/Authorization/GetPermissions?group=系统管理&amp;isActive=true&amp;page=1&amp;size=100
		/// 
		/// 所有查询参数都是可选的，可以按分组查看权限列表。
		/// </remarks>
		/// <response code="200">查询成功，返回分页的权限列表</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="500">服务器内部错误</response>
		[HttpGet]
		[ProducesResponseType(typeof(ApiResult<ApiPaging<PermissionDto>>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<ApiPaging<PermissionDto>>> GetPermissions([FromQuery] string? group = null, [FromQuery] bool? isActive = null, [FromQuery] int page = 1, [FromQuery] int size = 100)
		{
			try { var data = await _mediator.Send(new GetPermissionsListQuery { Group = group, IsActive = isActive, Page = page, Size = size }); return new ApiResult<ApiPaging<PermissionDto>> { Code = ApiEnum.查询成功, Message = "查询成功", data = data }; }
			catch (Exception ex) { return new ApiResult<ApiPaging<PermissionDto>> { Code = ApiEnum.查询失败, Message = ex.Message, data = null }; }
		}
	}
}
